package cn.ffcs.uec.organization.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Window;

import cn.ffcs.uec.organization.component.bean.UecOrganizationDeptsListboxExtBean;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecOrganizationDeptsListboxExt extends Div implements IdSpace {

    /**
     * 
     */
    private static final long serialVersionUID = 420632232685969802L;

    @Getter
    private UecOrganizationDeptsListboxExtBean bean = new UecOrganizationDeptsListboxExtBean();

    /**
     * zul.
     */
    private static final String zul = "/pages_nmg/uec_organization/comp/uecOrganizationDepts_listbox_ext.zul";

    private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
            .getBean("uecOrgDeptManager");

    private UecOrgDept uecOrgDept = new UecOrgDept();

    private OrganizationRelationVo organizationRelationVo;

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecOrganizationDeptsListboxExt() {
        // 1.绑定页面
        Executions.createComponents(zul, this, null);
        // 2.页面控件与java属性绑定
        Components.wireVariables(this, bean);
        // 3.绑定监听事件
        Components.addForwards(this, this, '$');
        // 转发
        this.addForward("onOrganizationDeptQuery", this, "onOrganizationDeptQueryResponse");
    }

    /**
     * 初始化
     */
    public void onCreate() {
        onQuery();
    }

    /**
     * 接受抛出的查询事件
     * 
     * @param event
     */
    public void onOrganizationDeptQueryResponse(final ForwardEvent event) {
        organizationRelationVo = (OrganizationRelationVo) event.getOrigin().getData();
        if (null != organizationRelationVo) {
            uecOrgDept = new UecOrgDept();
            if ("ORG".equals(organizationRelationVo.getOrgType())) {
                uecOrgDept.setPkOrg(organizationRelationVo.getOrg());
            } else {
                uecOrgDept.setPkOrg(null);
            }
            if ("DEPT".equals(organizationRelationVo.getOrgType())) {                
                uecOrgDept.setPkDept(organizationRelationVo.getOrg());
            } else {
                uecOrgDept.setPkDept(null);
            }
            bean.getOrgname().setValue(null);
            queryOrganization();
        }
    }

    /**
     * 触发选中事件
     */
    public void onSelectRequest() throws Exception {
        if (bean.getUecOrgDeptsListbox().getSelectedCount() > 0) {
            uecOrgDept = (UecOrgDept) bean.getUecOrgDeptsListbox().getSelectedItem().getValue();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", "edit");
            map.put("uecOrgDept", uecOrgDept);
            Window window = (Window) Executions
                    .createComponents("/pages_nmg/uec_organization/comp/uec_dept_info_main.zul", this, map);
            window.doModal();
            window.addEventListener("onOK", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getData() != null) {
                        uecOrgDept = (UecOrgDept) event.getData();
                        queryOrganization();
                    }
                }
            });
        }
    }

    /**
     * 查询
     */
    public void onQuery() {
        uecOrgDept = UecOrgDept.newInstance();
        uecOrgDept.setName(bean.getOrgname().getValue());
        this.queryOrganization();
    }

    /**
     * 查询
     */
    public void queryOrganization() {
        int activePage = bean.getUecOrgDeptsListboxPaging().getActivePage();
        int pageSize = bean.getUecOrgDeptsListboxPaging().getPageSize();
        PageInfo pageInfo = uecOrgDeptManager.queryAllColumnUecOrgDeptPage(uecOrgDept, activePage + 1, pageSize);
        // 总条数
        bean.getTotalSize().setValue(String.valueOf(pageInfo.getTotalCount()));
        ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
        bean.getUecOrgDeptsListbox().setModel(dataList);
        bean.getUecOrgDeptsListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
    }

    /**
     * 重置
     */
    public void onReset() {
        bean.getOrgname().setValue(null);
        bean.getTotalSize().setValue(null);
        clearVariable();
        ListboxUtils.clearListbox(this.bean.getUecOrgDeptsListbox());
        this.bean.getUecOrgDeptsListboxPaging().setTotalSize(1);
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecOrgDept = null;
    }

    /**
     * 翻页
     */
    public void onListboxPaging() {
        this.queryOrganization();
    }
}
